package com.bbm.bbmanage.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bbm.bbmanage.entity.*;
import com.bbm.bbmanage.entity.vo.PageVo;
import com.bbm.bbmanage.entity.vo.ResponseResult;
import com.bbm.bbmanage.mapper.UserMapper;
import com.bbm.bbmanage.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

import java.util.Objects;

/**
 * 用户表(User)表服务实现类
 *
 * @author lionkliu
 * @since 2023-11-23 14:33:55
 */
@Service
@Slf4j
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

    @Autowired
    private PasswordEncoder passwordEncoder;

    @Override
    public ResponseResult selectPage(Integer pageNum, Integer pageSize) {
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        Page<User> page = new Page<>(pageNum, pageSize);
        Page<User> userPage = page(page, queryWrapper);
        PageVo<User> pageVo = new PageVo<>();
        pageVo.setResult(userPage.getRecords());
        pageVo.setTotal(userPage.getTotal());
        pageVo.setPageTotal(pageVo.getPageTotal(pageSize));
        return ResponseResult.okResult(pageVo);
    }

    @Override
    public ResponseResult search(Integer pageNum, Integer pageSize, Integer userType, String username) {
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        if (!Objects.isNull(username)) {
            queryWrapper.like(User::getUsername, username);
        }
        if (!Objects.isNull(userType)) {
            queryWrapper.eq(User::getUserType, userType);
        }
        Page<User> userPage = new Page<>(pageNum, pageSize);
        page(userPage, queryWrapper);
        PageVo<User> userPageVo = new PageVo<>();
        userPageVo.setTotal(userPage.getTotal());
        userPageVo.setPageTotal(userPage.getPages());
        userPageVo.setResult(userPage.getRecords());

        return ResponseResult.okResult(userPageVo);
    }

    @Override
    public ResponseResult updateUserPassword(User user) {
        String finalPasswd = passwordEncoder.encode(user.getPassword());

        LambdaUpdateWrapper<User> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(User::getId, user.getId())
                .set(User::getPassword, finalPasswd);
        update(null, updateWrapper);
        return ResponseResult.okResult();
    }

    @Override
    public ResponseResult selectOne(Integer id) {
        return ResponseResult.okResult(getById(id));
    }

    @Override
    public ResponseResult insertOne(User user) {
        if (user.getPassword() != null) {
            String finalPasswd = passwordEncoder.encode(user.getPassword());
            user.setPassword(finalPasswd);
        }
        return save(user) ? ResponseResult.okResult() : ResponseResult.errorResult(AppHttpCodeEnum.INSERT_ERROR);
    }

    @Override
    public ResponseResult deleteById(Integer id) {
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(User::getId, id);
        remove(queryWrapper);
        return ResponseResult.okResult();
    }

    @Override
    public ResponseResult updateOne(User user) {
        LambdaUpdateWrapper<User> updateWrapper = new LambdaUpdateWrapper();
        updateWrapper.eq(User::getId, user.getId());
        log.info("{}", user);
        updateWrapper.set(User::getNickName, user.getNickName())
                .set(User::getPhoneNumber, user.getPhoneNumber())
                .set(User::getEmail, user.getEmail())
                .set(User::getAvatar, user.getAvatar())
                .set(User::getSex, user.getSex())
                .set(User::getStatus, user.getStatus())
                .set(User::getUserType, user.getUserType())
        ;
        return update(null, updateWrapper) ? ResponseResult.okResult() : ResponseResult.errorResult(AppHttpCodeEnum.UPDATE_ERROR);
    }

    @Override
    public ResponseResult updateUserInfo(User user) {
        LambdaUpdateWrapper<User> updateWrapper = new LambdaUpdateWrapper();
        updateWrapper.eq(User::getId, user.getId());
        log.info("{}", user);
        if (!Objects.isNull(user.getNickName())) {
            updateWrapper.set(User::getNickName, user.getNickName());
        }
        if (!Objects.isNull(user.getSex())) {
            updateWrapper.set(User::getSex, user.getSex());
        }
        if (!Objects.isNull(user.getPhoneNumber())) {
            updateWrapper.set(User::getPhoneNumber, user.getPhoneNumber());
        }
        if (!Objects.isNull(user.getEmail())) {
            updateWrapper.set(User::getEmail, user.getEmail());
        }
        if (!Objects.isNull(user.getAvatar())) {
            updateWrapper.set(User::getAvatar, user.getAvatar());
        }
        return update(null, updateWrapper) ? ResponseResult.okResult() : ResponseResult.errorResult(AppHttpCodeEnum.UPDATE_ERROR);
    }


}

